package com.example.controller;

import com.example.pojo.Emp;
import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_DaChiZi
 * Date: 2025/9/3
 * Time: 11:11
 */
@Slf4j
@RestController
public class EmpController {
    @Autowired
    EmpService empService;
    /**
     * 分页查询*/
//    @GetMapping("/emps1")
//    public Result pageFind(@RequestParam(defaultValue = "1") Integer page,
//                           @RequestParam(defaultValue = "10") Integer pageSize) {
//        PageBean pageBean =  empService.page(page,pageSize);
//        return Result.success(pageBean);
//    }


    @GetMapping("/emps")
    public Result pageFind(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer pageSize,
                           String name, Short gender,
                           @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime begin,
                           @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime end) {
        PageBean pageBean =  empService.page(page,pageSize,name,gender,begin,end);
        return Result.success(pageBean);
    }

    @DeleteMapping("/emps/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids) {
            empService.deleteByIds(ids);
        return Result.success();
    }

    @PostMapping("/emps")
    public Result insertEmp(@RequestBody Emp emp) {
        empService.insertEmp(emp);
        return Result.success();
    }

    @GetMapping("/emps/{id}")
    public Result findById(@PathVariable Integer id) {
        Emp emp =  empService.findById(id);
        return Result.success(emp);
    }

    @PutMapping("/emps")
    public Result updateById(@RequestBody Emp emp) {
        empService.updateById(emp);
        return Result.success();
    }
}
